package com.ruoyi.module.zxm.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.module.zxm.service.GasService;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.service.*;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;


import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

@RestController
@RequestMapping("/gas")
public class GasController extends BaseController {

    @Autowired
    private GasService gasService;
    @Autowired
    private IYhBaseinfoService yhBaseinfoService;
    @Autowired
    private IYhFillingService yhFillingService;
    @Autowired
    private IYhGasCylindersService yhGasCylindersService;
    @Autowired
    private ISysRoleService  sysRoleService;
    @Autowired
    private IYhYinspectionService yhYinspectionService;
    @Resource
    private RedisCache redisCache;
    /**
     * 查询充装信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:filling:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhFilling yhFilling)
    {
        startPage();
        List<YhFilling> list = gasService.selectYhFillingList(yhFilling);
        return getDataTable(list);
    }

    @RequestMapping("/search")
    public AjaxResult list1(Long id)
    {
        //System.out.println(id+"lkj");
        //充气
        SysUserRole sysRole =  sysRoleService.selectRoleByUser(getUserId());
        Long roleId = sysRole.getRoleId();
        if (roleId==4){
                YhGasCylinders yhGasCylinders = yhGasCylindersService.selectYhGasCylindersById(id);
                yhGasCylinders.setUserId(roleId);
                YhBaseinfo yhBaseinfo =  yhBaseinfoService.selectYhBaseinfoByUserId(getUserId());
                yhGasCylinders.setName(yhBaseinfo.getName());
                yhGasCylinders.setWorkPlace(yhBaseinfo.getWorkPlace());
                yhGasCylinders.setUserName(yhBaseinfo.getUserName());
                yhGasCylinders.setNote4(yhBaseinfo.getWorkPlace());
                yhGasCylinders.setNote5(yhGasCylinders.getGasMedia());
                return AjaxResult.success(yhGasCylinders);

        }
        else if (roleId==7){

            YhBaseinfo yhBaseinfo  =  yhBaseinfoService.selectYhBaseinfoByUserId(getUserId());
            YhGasCylinders  yhGasCylinders =   yhGasCylindersService.selectYhGasCylindersById(id);
            //向检测对象表传值

                YhYinspection yhYinspection = new YhYinspection();
                yhYinspection.setName(yhBaseinfo.getName());
                yhYinspection.setWorkPlace(yhBaseinfo.getWorkPlace());
                yhYinspection.setUserName(yhBaseinfo.getUserName());
                //充装表
                yhYinspection.setCylinderNumber(yhGasCylinders.getCylinderNumber());
                yhYinspection.setCylinderStatus(yhGasCylinders.getCylinderStatus());
                yhYinspection.setGasMedia(yhGasCylinders.getGasMedia());
                yhYinspection.setBelongingGasStation(yhGasCylinders.getBelongingGasStation());
                yhYinspection.setManufactureDate(yhGasCylinders.getManufactureDate());
                yhYinspection.setScrapDate(yhGasCylinders.getScrapDate());
                yhYinspection.setUserId(7L);
                yhYinspection.setManufacturingUnits(yhGasCylinders.getManufacturingUnits());
                yhYinspection.setNote2(String.valueOf(yhGasCylinders.getId()));
                yhYinspection.setVisualInspertion("0");

                yhYinspection.setExportThreadStyle("0");
                yhYinspection.setSealingSituation("0");
                yhYinspection.setDeformation("0");
                yhYinspection.setBottleTemperatureAbnormalities("0");
                return AjaxResult.success(yhYinspection);

        }
        else if (roleId==3){
            //建档
            //Long id1 = 1L;
            YhBaseinfo yhBaseinfo  =  yhBaseinfoService.selectYhBaseinfoByUserId(getUserId());
            YhGasCylinders  yhGasCylinders =   yhGasCylindersService.selectYhGasCylindersById(id);
            if (yhGasCylinders.getGasMedia()!=null){
                yhGasCylinders.setName(yhBaseinfo.getName());
                yhGasCylinders.setWorkPlace(yhBaseinfo.getWorkPlace());
                yhGasCylinders.setUserName(yhBaseinfo.getUserName());
                yhGasCylinders.setUserId(3L);
                return AjaxResult.success(yhGasCylinders);
            }
            else {
                yhBaseinfo.setNote1("1");
                yhBaseinfo.setUserId(3L);
                return AjaxResult.success(yhBaseinfo);
            }


        }

        return null;

    }

//    private void extracted(YhBaseinfo yhBaseinfo) {
//        if (yhBaseinfo.getWorkPlace().equals("锦州市御龙气体有限公司")){
//            yhBaseinfo.setWorkPlace("101");
//        }
//        else if (yhBaseinfo.getWorkPlace().equals("锦州市宏远溶解乙炔有限责任公司")){
//            yhBaseinfo.setWorkPlace("102");
//        }
//        else if (yhBaseinfo.getWorkPlace().equals("凌海金晟气体有限公司")){
//            yhBaseinfo.setWorkPlace("103");
//        }
//        else if (yhBaseinfo.getWorkPlace().equals("锦州市天赢气体有限公司")){
//            yhBaseinfo.setWorkPlace("104");
//        }
//        else if (yhBaseinfo.getWorkPlace().equals("锦州市全福气体有限公司")){
//            yhBaseinfo.setWorkPlace("105");
//        }
//    }

    //根据角色判断
    @RequestMapping("/getinfo")
    public AjaxResult getinfo(){
       SysUserRole sysRole =  sysRoleService.selectRoleByUser(getUserId());
       Long roleId =  sysRole.getRoleId();

        return AjaxResult.success(sysRole);
    }

    @RequestMapping("/update")
    //建档的更新操作
    public AjaxResult insertlist2(@RequestBody YhGasCylinders yhGasCylinders){
        //System.out.println(yhGasCylinders+"444");

        yhGasCylinders.setDeptId(getDeptId());
        yhGasCylinders.setUserId(getUserId());
        return toAjax(yhGasCylindersService.updateYhGasCylinders1(yhGasCylinders));
    }

    @RequestMapping("/insert")
    //充气
    public AjaxResult insertlist3(@RequestBody YhFilling yhFilling){

        //System.out.println(yhFilling+"oko");
        yhFilling.setFillingMedium(yhFilling.getNote5());
        yhFilling.setFillingStation(yhFilling.getNote4());
        yhFilling.setUserId(getUserId());
        yhFilling.setDeptId(getDeptId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(new Date());
        yhFilling.setNote1(dateStr);
        return toAjax(yhFillingService.insertYhFilling(yhFilling));
    }
    @RequestMapping("/insert1")
    public AjaxResult insertlist4(@RequestBody YhYinspection yhYinspection){

        yhYinspection.setUserId(getUserId());
        yhYinspection.setDeptId(getDeptId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateStr = sdf.format(new Date());
        yhYinspection.setNote1(dateStr);
        return toAjax(yhYinspectionService.insertYhYinspection(yhYinspection));
    }

    //查询最近的充装信息
    @RequestMapping("/recent")
    public  AjaxResult recent (Long id){
      List<YhFilling> result  =    yhFillingService.selectYhFillingByList(id);
      return AjaxResult.success(result);
    }
    //查询气瓶建档信息
    @GetMapping("/look")
    public AjaxResult look (){
        YhGasCylinders yhGasCylinders = new YhGasCylinders();
        ArrayList<YhGasCylinders> list1 = new ArrayList<>();
        List<YhGasCylinders> list = yhGasCylindersService.selectYhGasCylindersList1(yhGasCylinders);
        for (YhGasCylinders a:list){
            if (a.getNote5()!=null){
                list1.add(a);
            }
        }
        return AjaxResult.success(list1);
    }
    //查询气瓶充装信息
    @RequestMapping("/fille")
    public AjaxResult  fille(YhFilling yhFilling ){
        //startPage();
        //YhFilling yhFilling = new YhFilling();
        List<YhFilling> list  =  yhFillingService.selectYhFillingList1(yhFilling);
        return AjaxResult.success(list);
//       List<YhFilling> honey = redisCache.getCacheList("honey");
//        if (honey.size()>0){
//            System.out.println("123");
//            return AjaxResult.success(honey);
//        }
//        else {
//            List<YhFilling> list  =  yhFillingService.selectYhFillingList1(yhFilling);
//            redisCache.setCacheList("honey",list);
//            redisCache.expire("honey",20, TimeUnit.SECONDS);
//            return AjaxResult.success(list);
//        }

    }
    //查询气瓶详细
    @RequestMapping("/full")
    public AjaxResult  full(Long id ){
        YhFilling yhFilling  =  yhFillingService.selectYhFillingById(id);
        return AjaxResult.success(yhFilling);
    }
    //查询最近建档信息
    @RequestMapping("/build")
    public AjaxResult  build(Long id ){
        YhGasCylinders yhGasCylinders  =  yhGasCylindersService.selectYhGasCylindersByBuild(id);
        return AjaxResult.success(yhGasCylinders);
    }
    //查询充装详细
    @RequestMapping("/defille")
    public AjaxResult  defille(Long id ){
        YhFilling yhFilling  =  yhFillingService.selectYhFillingById1(id);
        return AjaxResult.success(yhFilling);
    }

    //查询充装详细1
    @RequestMapping("/defille1")
    public AjaxResult  defille1(Long id ){
        // 先查钢瓶信息表
        YhGasCylinders yhGasCylinders = yhGasCylindersService.selectYhGasCylindersById(id);
//        System.out.println(yhGasCylinders.toString());
        YhFilling yhFilling  =  new YhFilling();
        // 如果存在充装数据，直接给就行
        if(yhFillingService.selectYhFillingById1(id)!=null)yhFilling=yhFillingService.selectYhFillingById1(id);
        yhFilling.setCylinderNumber(yhGasCylinders.getCylinderNumber());// 钢瓶编号
        yhFilling.setBelongingGasStation(yhGasCylinders.getBelongingGasStation()); // 所属气站
        yhFilling.setVolume(yhGasCylinders.getVolume());// 钢瓶容积
        yhFilling.setWeight(yhGasCylinders.getWeight());// 钢瓶重量
        yhFilling.setManufactureDate(yhGasCylinders.getManufactureDate());// 生产日期
        yhFilling.setScrapDate(yhGasCylinders.getScrapDate());// 报废日期
        yhFilling.setInspectionTime(yhGasCylinders.getInspectionTime());// 气瓶检测日期
        yhFilling.setNextInspectionTime(yhGasCylinders.getNextInspectionTime());// 下次检测日期
        yhFilling.setCylinderStatus(yhGasCylinders.getCylinderStatus());// 钢瓶状态
        yhFilling.setGasMedia(yhGasCylinders.getGasMedia());// 应充介质
        return AjaxResult.success(yhFilling);
    }

    //查询检查详细
    @RequestMapping("/spect")
    public AjaxResult  spect(Long id ){
        YhYinspection yinspection = yhYinspectionService.selectYhYinspectionBySpect(id);
        return AjaxResult.success(yinspection);
    }


    //查询气瓶检查信息
    @RequestMapping("/check")
    public AjaxResult  check(YhYinspection yhYinspection ){
        //startPage();
        //YhFilling yhFilling = new YhFilling();
        List<YhYinspection> list = yhYinspectionService.selectYhYinspectionList1(yhYinspection);
        return AjaxResult.success(list);
    }
    //查询气瓶检查详细
    @RequestMapping("/check1")
    public AjaxResult  acheck(Long id ){
        YhYinspection yhYinspection = yhYinspectionService.selectYhYinspectionById(id);
        return AjaxResult.success(yhYinspection);
    }


    //查询建档详细
    @RequestMapping("/detail")
    public  AjaxResult detail(Long id){

       YhGasCylinders yhGasCylinders = yhGasCylindersService.selectYhGasCylindersById(id);
       return AjaxResult.success(yhGasCylinders);
    }
    //解码方法
    @RequestMapping("/encode")
    public AjaxResult encode (String text){
        //System.out.println(text+"ll");
        ArrayList<Object> list = new ArrayList<>();
        byte[] bt=text.getBytes(); //将需要加密的内容转换为字节数组
        for(int i=0;i<bt.length;i++)

        {
            bt[i]=(byte)(bt[i]^10); //通过异或运算进行加密
        }
        String newresult=new String(bt,0,bt.length); //将加密后的字符串保存到 newresult变量中
        list.add(newresult);
        System.out.println(newresult);
        return AjaxResult.success(list);
    }

    /**
     * 导出充装信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:filling:export')")
    @Log(title = "充装信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhFilling yhFilling)
    {
        List<YhFilling> list = gasService.selectYhFillingList(yhFilling);
        ExcelUtil<YhFilling> util = new ExcelUtil<YhFilling>(YhFilling.class);
        util.exportExcel(response, list, "充装信息数据");
    }

    /**
     * 获取充装信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:filling:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(gasService.selectYhFillingById(id));
    }

    /**
     * 新增充装信息
     */
    @PreAuthorize("@ss.hasPermi('system:filling:add')")
    @Log(title = "充装信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhFilling yhFilling)
    {
        return toAjax(gasService.insertYhFilling(yhFilling));
    }

    /**
     * 修改充装信息
     */
    @PreAuthorize("@ss.hasPermi('system:filling:edit')")
    @Log(title = "充装信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhFilling yhFilling)
    {
        return toAjax(gasService.updateYhFilling(yhFilling));
    }

    /**
     * 删除充装信息
     */
    @PreAuthorize("@ss.hasPermi('system:filling:remove')")
    @Log(title = "充装信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(gasService.deleteYhFillingByIds(ids));
    }
}
